<template>
  <div>
    <div style="margin: 10px 0">
      <el-input
        style="width: 200px"
        placeholder="请输入名称"
        v-model="username"
        suffix-icon="avatar"
      ></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <div style="margin: 10px 0; display: inline-flex">
      <el-upload
        :show-file-list="false"
        accept="xlsx"
        :on-success="handleFileUploadSuccess"
        action="http://localhost:9090/file/upload/"
        :limit="1"
        style="display: inline-block"
      >
        <el-button type="primary" icon="top" class="ml-5">上传文件</el-button>
      </el-upload>
      <el-popconfirm
        width="200px"
        confirmButtonText="确定"
        cancelButtonText="取消"
        icon="check"
        iconColor="red"
        title="您确定批量删除吗？"
        @confirm="delBatch()"
      >
        <template #reference>
          <el-button type="danger" icon="remove" class="ml-5"
            >批量删除</el-button
          >
        </template>
      </el-popconfirm>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      header-cell-class-name="headerBg"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="ID" width="80"> </el-table-column>
      <el-table-column prop="name" label="文件名称"> </el-table-column>
      <el-table-column prop="type" label="文件类型"> </el-table-column>
      <el-table-column prop="size" label="文件大小(kb)"> </el-table-column>
      <el-table-column label="下载">
        <template #default="scope">
          <el-button type="primary" @click="download(scope.row.url)"
            >下载</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="启用">
        <template #default="scope">
          <el-switch
            v-model="scope.row.enable"
            active-color="#13ce66"
            inactive-color="#ccc"
            @change="changeEnable(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="success" icon="edit" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="确定"
            cancelButtonText="取消"
            icon="check"
            iconColor="red"
            title="您确定删除吗？"
            @confirm="del(scope.row.id)"
          >
            <template #reference>
              <el-button type="danger" icon="delete">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { serverIp } from "../../public/config";

export default {
  name: "File",
  data() {
    return {
      serverIp: serverIp,
      tableData: [],
      username: "",
      multipleTable: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request
        .get("/file/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            username: this.username,
          },
        })
        .then((response) => {
          //console.log(response);
          this.tableData = response.data.records;
          this.total = response.data.total;
        });
    },
    reset() {
      this.username = "";
      this.load();
    },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum;
      this.load();
    },
    randomTb() {
      this.form.username = Math.random().toString(36).substring(2);
      this.form.nickname = Math.random().toString(36).substring(2);
      this.form.email = Math.random().toString(36).substring(2);
      this.form.phone = Math.random().toString(36).substring(2);
      this.form.address = Math.random().toString(36).substring(2);
      console.log(this.form);
    },
    del(id) {
      this.request.delete("/file/" + id, this.form).then((response) => {
        if (response.code === "200") {
          this.$message.success("删除成功"); //顶部弹出提示框。
          this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error("删除失败");
        }
        console.log(response.data);
      });
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleTable = val;
    },
    delBatch() {
      //把对象数组。变成纯ID数组。
      let ids = this.multipleTable.map((item) => item.id); //[{},{},{}] =>  [1,2,3]
      this.request.post("/file/del/batch", ids).then((response) => {
        if (response.code === "200") {
          this.$message.success("批量删除成功"); //顶部弹出提示框。
          //this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error("批量删除失败");
        }
        console.log(response.data);
      });
    },
    handleFileUploadSuccess(res) {
      console.log(res);
      this.load();
    },
    download(url) {
      window.open(url);
    },
    changeEnable(row) {
      this.request.post("/file/update", row).then((res) => {
        if (res.code === "200") {
          this.$message.success("操作成功");
        }
      });
    },
  },
};
</script>
<style scoped></style>
